Ext.ns("OutRecordsList");
OutRecordsList = Ext.extend(Ext.Panel, {
	searchPanel : null,
	gridPanel : null,
	store : null,
	topbar : null,
	layout : "form",
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		OutRecordsList.superclass.constructor.call(this, {
					id : "OutRecordsList",
					title : "出库单列表",
					iconCls : "menu-car_apply",
					items : [this.searchPanel, this.gridPanel]
				});
	},
	initUIComponents : function() {
		this.searchPanel = new Ext.FormPanel({
					id : "OutRecordsSearchForm",
					height : 40,
					region : "north",
					frame : false,
					border : false,
					layout : "hbox",
					layoutConfig : {
						padding : "5",
						align : "middle"
					},
					defaults : {
						xtype : "label",
						margins : {
							top : 0,
							right : 4,
							bottom : 4,
							left : 4
						}
					},
					items : [{
								text : "请输入查询条件:"
							}, {
								text : "生产编号"
							}, {
								xtype : "textfield",
								id : 'productcode',
								name : "outRecords.productcode"
							}, {
								text : "出库单编号"
							}, {
								xtype : "textfield",
								id : 'outcode',
								name : "outRecords.outcode"
							}, {
								xtype : "button",
								text : "查询",
								iconCls : "btn-search",
								handler : function() {
									var c = Ext.getCmp("OutRecordsSearchForm");
									var d = Ext.getCmp("maingrid");
									var s = d.getStore();
									if (c.getForm().isValid()) {
										s.setBaseParam(
												'outRecords.productcode', Ext
														.getCmp("productcode")
														.getValue());
										s.setBaseParam('outRecords.outcode',
												Ext.getCmp("outcode")
														.getValue());
										s.load();
									}
								}
							}]
				});
		this.store = new Ext.data.JsonStore({
					autoLoad : true,
					url : 'listOutRecords.do',
					paramNames : {
						start : 'start',
						limit : 'rownum'
					},// 重命名分页参数以和action中对象对应
					root : 'list',
					totalProperty : 'totalCount',
					idProperty : 'id',
					remoteSort : true,
					fields : ['outcode', 'id', 'productcode', 'outtime',
							'opzh', 'pickman', 'remark', 'deleteflag']
				});
		this.store.setDefaultSort('id', 'desc');
		var b = new Ext.grid.CheckboxSelectionModel();
		var a = new Ext.grid.ColumnModel({
			columns : [b, {
						header : "状态",
						dataIndex : 'deleteflag',
						width : 20,
						renderer : function(v) {
							return v == 1 ? "正常" : "已作废";
						}
					}, {
						header : "生产编号",
						dataIndex : 'productcode',
						width : 30,
						sortable : true
					}, {
						header : "出库单编号",
						dataIndex : 'outcode',
						width : 30,
						sortable : true
					}, {
						header : "出库时间",
						dataIndex : 'outtime',
						width : 40,
						align : 'left',
						sortable : true
					}, {
						header : "操作人",
						dataIndex : 'opzh',
						width : 30,
						align : 'left',
						sortable : true
					}, {
						header : "提货人",
						dataIndex : 'pickman',
						width : 30,
						align : 'left',
						sortable : true
					}, {
						header : "备注",
						dataIndex : 'remark',
						width : 100,
						align : 'left',
						sortable : true
					}, {
						header : "管理",
						dataIndex : "id",
						width : 50,
						sortable : false,
						renderer : function(g, f, d, j, e) {
							var i = d.data.productcode;
							var h = "";
							if (isGranted("printDetail")) {
								h += '&nbsp;<button title="打印详单" value=" " class="btn-print" onclick="printOutRecords(\''
										+ i + '\',2)">&nbsp;</button>';
							}
							if (isGranted("app.OutRecordsAction.saveOutRecords")) {
								h += '&nbsp;<button title="编辑出库单" value=" " class="btn-edit" onclick="OutRecordsList.edit(\''
										+ i + '\')">&nbsp;</button>';
							}
							if (isGranted("app.OutRecordsAction.delOutRecords")) {
								h += '&nbsp;<button title="删除出库单" value=" " class="btn-del" onclick="OutRecordsList.del(\''
										+ i + '\')">&nbsp;</button>';
							}
							return h;
						}
					}],
			defaults : {
				sortable : true,
				menuDisabled : false,
				width : 100
			}
		});
		this.topbar = new Ext.Toolbar({
					id : "OutRecordsAdminFootBar",
					height : 30,
					bodyStyle : "text-align:left",
					items : []
				});
		if (isGranted("printList")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-print",
						text : "打印出库清单",
						handler : function() {
							var e = Ext.getCmp("maingrid");
							var c = e.getSelectionModel().getSelections();
							if (c.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择要打印的记录！");
								return;
							}
							var f = Array();
							for (var d = 0; d < c.length; d++) {
								f.push(c[d].data.productcode);
							}
							printOutRecords(f.join(), 1);
						}
					}));
		}
		this.gridPanel = new Ext.grid.GridPanel({
					id : "maingrid",
					tbar : this.topbar,
					region : "center",
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					cm : a,
					sm : b,
					viewConfig : {
						forceFit : true,
						enableRowBody : false,
						showPreview : false
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
				});
		this.gridPanel.addListener("rowdblclick", function(d, c, f) {
					d.getSelectionModel().each(function(e) {
								new InRecordsForm({
											productid : e.data.productcode
										}).show();
							});
				});
	}
});
OutRecordsList.edit = function(a) {
	new OutRecordsForm({
				productcode : a
			}).show();
}
OutRecordsList.del = function(b) {
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
				if (c == "yes") {
					Ext.Ajax.request({
								url : _ctxPath + "delOutRecords.do?op=ajax",
								params : {
									'ids' : b
								},
								method : "post",
								success : function(e) {
									var d = Ext.util.JSON
											.decode(e.responseText);
									if (d.success) {
										Ext.Msg.alert("信息提示", "成功删除所选记录！");
									} else {
										Ext.Msg.alert("信息提示", d.message);
									}
									Ext.getCmp("maingrid").getStore().reload({
												params : {
													start : 0,
													limit : 25
												}
											});
								}
							});
				}
			});
}
var printOutRecords = function(productcode, type) {
	var win = new Ext.Window({
				modal : true,
				width : 950,
				height : 500,
				title : '打印出库清单',
				items : []
			});
	if (type == 1) {
		win.items.add(new Ext.Panel({
			height : 400,
			html : "<iframe src='ReportServer?reportlet=outRecords.cpt&productcode="
					+ productcode
					+ "' style='width:100%;height:100%' frameborder='0' marginwidth='0' marginheight='0'></iframe>"
		}))
	} else if (type == 2) {
		win.setTitle("打印详单");
		win.items.add(new Ext.Panel({
			height : 400,
			html : "<iframe src='ReportServer?reportlet=outRecordsDetail.cpt&productcode="
					+ productcode
					+ "' style='width:100%;height:100%' frameborder='0' marginwidth='0' marginheight='0'></iframe>"
		}))
	}
	win.show();
}